package com.fattyCorps.common.packet.hero;

import com.alibaba.fastjson.annotation.JSONField;
import com.fattyCorps.common.obj.config.HeroMould;
import com.fattyCorps.common.packet.hero.component.Component;
import com.fattyCorps.common.packet.hero.component.skill.HeroSkill;
import com.fattyCorps.common.packet.PacketElem;
import com.fattyCorps.common.packet.hero.component.prop.HeroProp;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Data
@Slf4j
@NoArgsConstructor
public class Hero extends PacketElem {
    private Integer typeId = 0;
    private String heroName = ""; // 自定义昵称
    private Integer star = 1; // 星级
//    private Integer quality; // 品阶 不需要了
    private Integer lv = 1;

    // 组件
    @JSONField(serialize = false)
    private Component.Trunk trunk = null;

    public Component.Trunk getTrunk() {
        if (trunk == null) {
            init();
            trunk.onUpdate();
        }

        return trunk;
    }

    public void init() {
        trunk = new Component.Trunk(this);
        trunk.addComponent(HeroSkill.class);

        // 属性计算放在最后，属性依赖其他组件更新后的结果
        trunk.addComponent(HeroProp.class);
    }

    @JSONField(serialize = false)
    public HeroProp getPropComp() {
        if (trunk == null) {
            init();
        }

        return trunk.getComponent(HeroProp.class);
    }

    @JSONField(serialize = false)
    public HeroSkill getSkillComp() {
        return trunk.getComponent(HeroSkill.class);
    }

    // 升级
    public void lvUp(int step) {
        lv += step;
        update();
    }

    public void setLvAndUpdate(int lv) {
        this.lv = lv;
        update();
    }

    // 升星
    public void starUp(int step) {
        star += step;
        update();
    }

    public void setStarAndUpdate(int star) {
        this.star = star;
        update();
    }

    public static Hero makeHero(HeroMould heroMould) {
        Hero hero = new Hero();
        hero.setTypeId(heroMould.getId());
        hero.setHeroName(heroMould.getName());
        hero.setLv(1);
        hero.setStar(heroMould.getInitStar());
        hero.update();
        return hero;
    }

    public void update() {
        if (trunk == null) {
            init();
        }

        trunk.onUpdate();
    }

//    @Override
//    public Object clone() throws CloneNotSupportedException {
//        Hero objClone =  (Hero) super.clone();
//        objClone.trunk = trunk.clone();
//        return clone
//    }
}
